| Conditions | 7 |
| Total Lines | 63 |
| Code Lines | 50 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | document.addEventListener("DOMContentLoaded", function() { |
||
| 2 | (function timeAgo(selector) { |
||
|
|
|||
| 3 | |||
| 4 | var templates = { |
||
| 5 | prefix: "", |
||
| 6 | suffix: " ago", |
||
| 7 | seconds: "less than a minute", |
||
| 8 | minute: "about a minute", |
||
| 9 | minutes: "%d minutes", |
||
| 10 | hour: "about an hour", |
||
| 11 | hours: "about %d hours", |
||
| 12 | day: "a day", |
||
| 13 | days: "%d days", |
||
| 14 | month: "about a month", |
||
| 15 | months: "%d months", |
||
| 16 | year: "about a year", |
||
| 17 | years: "%d years" |
||
| 18 | }; |
||
| 19 | var template = function(t, n) { |
||
| 20 | return templates[t] && templates[t].replace(/%d/i, Math.abs(Math.round(n))); |
||
| 21 | }; |
||
| 22 | |||
| 23 | var timer = function(time) { |
||
| 24 | if (!time) |
||
| 25 | return; |
||
| 26 | time = time.replace(/\.\d+/, ""); // remove milliseconds |
||
| 27 | time = time.replace(/-/, "/").replace(/-/, "/"); |
||
| 28 | time = time.replace(/T/, " ").replace(/Z/, " UTC"); |
||
| 29 | time = time.replace(/([\+\-]\d\d)\:?(\d\d)/, " $1$2"); // -04:00 -> -0400 |
||
| 30 | time = new Date(time * 1000 || time); |
||
| 31 | |||
| 32 | var now = new Date(); |
||
| 33 | var seconds = ((now.getTime() - time) * .001) >> 0; |
||
| 34 | var minutes = seconds / 60; |
||
| 35 | var hours = minutes / 60; |
||
| 36 | var days = hours / 24; |
||
| 37 | var years = days / 365; |
||
| 38 | |||
| 39 | return templates.prefix + ( |
||
| 40 | seconds < 45 && template('seconds', seconds) || |
||
| 41 | seconds < 90 && template('minute', 1) || |
||
| 42 | minutes < 45 && template('minutes', minutes) || |
||
| 43 | minutes < 90 && template('hour', 1) || |
||
| 44 | hours < 24 && template('hours', hours) || |
||
| 45 | hours < 42 && template('day', 1) || |
||
| 46 | days < 30 && template('days', days) || |
||
| 47 | days < 45 && template('month', 1) || |
||
| 48 | days < 365 && template('months', days / 30) || |
||
| 49 | years < 1.5 && template('year', 1) || |
||
| 50 | template('years', years) |
||
| 51 | ) + templates.suffix; |
||
| 52 | }; |
||
| 53 | |||
| 54 | var elements = document.getElementsByClassName('timeago'); |
||
| 55 | for (var i = 0; i < elements.length; ++i) { |
||
| 56 | var $this = elements[i]; |
||
| 57 | if (typeof $this === 'object') { |
||
| 58 | $this.innerHTML = timer($this.getAttribute('data-datetime')); |
||
| 59 | } |
||
| 60 | } |
||
| 61 | // update time every minute |
||
| 62 | setTimeout(timeAgo, 60000); |
||
| 63 | |||
| 64 | })(); |
||
| 65 | |||
| 67 | }); |
This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.